<?php
defined('BASEPATH') OR exit('No direct script access allowed');

include_once 'adminBase.php';

class Article extends AdminBase{
    
    
    public function index($now_page=""){
        $now_page = $now_page ? $now_page:0;
        
        $pagination     = $this->config->item('pagination');
        $pagination['base_url']   = "/article/index/";
        $this->db->select('id');
        $pagination['total_rows'] = $this->db->get('article')->num_rows();
        $this->load->library('pagination');
        $this->pagination->initialize($pagination); 
        $this->load->vars('page',$this->pagination->create_links());
        
        $this->db->select('id,title,time,type');
        $this->db->order_by('id','desc');
        $this->load->vars("data",$this->db->get('article',$pagination['per_page'],$now_page)->result_array());
        
        $this->load->vars('title','文章管理');
        $this->load->vars('description','文章列表管理');
        $this->load->vars('load_tempalte','admin/article_index.php');
        
        $this->load->view('public/layout.php');
    }
    
    
    
    public function post(){
        
       $id          = $this->input->post('id');
       $title       = $this->input->post('title',true);
       $countent    = $this->input->post('countent',true);
       $hct         = $this->input->post('hct',true);
       
       if(empty($id))
       {
           if($title && $countent){
               $insert = $this->db->insert('article',['title'=>$title,"countent"=>$countent,'time'=>time()]);
               if($insert){
                   if($hct){
                       echo '操作成功';
                   }else{
                      jump('/article/from?result=ok');
                   }
               }else{
                   
                   if($hct){
                       echo '操作失败';
                   }else{
                        jump('/article/from?result=no');
                   }
               }
           }else{
                        jump('/article/from?result=no');
           }
       }else{
           if($title && $countent){
               $is_up = $this->db->update('article', ['title'=>$title,"countent"=>$countent], array('id' => $id));
               if($is_up){
                   if($hct){
                       echo '操作成功';
                   }else{
                       jump('/article/from/'.$id.'?result=ok');
                   }
               }else{
                   
                   if($hct){
                       echo '操作失败';
                   }else{
                       jump('/article/from'.$id.'?result=no');
                   }
               }
           }else{
               jump('/article/from'.$id.'?result=no');
           }
       }
        
    }
    
    
    public function delete($id = ""){
        if(!empty($id)){
           $is_ok =  $this->db->delete('article',array('id'=>$id));
           if($is_ok){
               return_json([],'操作成功',1);
           }else{
               return_json([],'操作失败',-1);
           }
        }
    }
    
    public function from($id = ""){
        
        $this->load->vars('title','内容管理');
        $this->load->vars('description','文章发布管理');
        $this->load->vars('load_tempalte','admin/article_from.php');
        
        if(!empty($id)){
            $resutl = $this->db->get_where('article',array('id'=>$id))->result_array();
            if($resutl){
                $this->load->vars('data',$resutl[0]);
            }
        }
        
        $this->load->view('public/layout.php');
    }
    
    
}